package com.hxci.pointmap.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

public class CaptchaUtil {
    // 验证码字符集
    private static final char[] chars = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };

    // 随机数生成器
    private static Random random = new Random();

    // 生成随机验证码
    public static String generateRandomCode(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(chars[random.nextInt(chars.length)]);
        }
        return sb.toString();
    }

    // 生成验证码图片
    public static BufferedImage generateImage(String code, int width, int height) {
        // 创建图像
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();

        // 设置渲染提示
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        // 设置背景色
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);

        // 设置字体
        g.setFont(new Font("Arial", Font.BOLD, height * 3 / 4));

        // 绘制干扰线
        drawInterferenceLines(g, width, height);

        // 绘制验证码
        drawCode(g, code, width, height);

        // 绘制干扰点
        drawInterferencePoints(g, width, height);

        g.dispose();
        return image;
    }

    // 绘制干扰线
    private static void drawInterferenceLines(Graphics2D g, int width, int height) {
        int lineCount = 5 + random.nextInt(5); // 5-10条线
        for (int i = 0; i < lineCount; i++) {
            g.setColor(getRandomColor(150, 200));
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }
    }

    // 绘制验证码
    private static void drawCode(Graphics2D g, String code, int width, int height) {
        int charWidth = width / (code.length() + 1);
        for (int i = 0; i < code.length(); i++) {
            // 随机颜色
            g.setColor(getRandomColor(50, 150));

            // 随机旋转角度
            double angle = (random.nextInt(30) - 15) * Math.PI / 180;
            g.rotate(angle, charWidth * (i + 1), height / 2);

            // 绘制字符
            g.drawString(String.valueOf(code.charAt(i)), charWidth * (i + 1) - charWidth / 3, height * 3 / 4);

            // 恢复旋转
            g.rotate(-angle, charWidth * (i + 1), height / 2);
        }
    }

    // 绘制干扰点
    private static void drawInterferencePoints(Graphics2D g, int width, int height) {
        int pointCount = 50 + random.nextInt(50); // 50-100个点
        for (int i = 0; i < pointCount; i++) {
            g.setColor(getRandomColor(150, 200));
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            g.fillOval(x, y, 1, 1);
        }
    }

    // 获取随机颜色
    private static Color getRandomColor(int min, int max) {
        int r = min + random.nextInt(max - min);
        int g = min + random.nextInt(max - min);
        int b = min + random.nextInt(max - min);
        return new Color(r, g, b);
    }

    // 输出验证码图片到输出流
    public static void outputImage(BufferedImage image, OutputStream out) throws IOException {
        ImageIO.write(image, "JPEG", out);
    }
}
